package com.xhwl.logistics.enums;

import com.xhwl.common.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

@Getter
@AllArgsConstructor
public enum VehicleCardTypeEnum {

    FREE(0, "免费卡", "freeCard"),
    MONTH(1, "月卡", "monthCard"),
    STORE(2, "储值卡", "storedValueCard"),
    TEMPORARILY(3, "临时卡", "temporaryCard"),
    VISITOR(4, "访客卡", "visitorCard"),   //细滘项目引入的车卡类型

    VIP(5, "VIP车", "vipCar"),               //中建项目引入的车卡类型，富士车卡类型
    HOUR_RENT(6, "时租车", "hourRentCar"),         //中建项目引入的车卡类型，富士车卡类型
    PROPERTY_RIGHT(7, "产权车", "propertyRightCar"),    //中建项目引入的车卡类型
    COUNTING(8, "计次车", "countingCar"),          //中建项目引入的车卡类型
    HONORED_GUEST(9, "贵宾卡", "honoredGuestCard"),     //中建项目引入的车卡类型
    EMPLOYEE(10, "员工卡", "employeeCard"),         //中建项目引入的车卡类型

    TEMPORARY_BOOKING(11, "临时预约车", "temporaryBookingCar"),     //中建项目引入的车卡类型
    OWNER_TEMPORARY(12, "业主临时车", "ownerTemporaryCar"),       //中建项目引入的车卡类型

    FULL_MONTH(13,"全时段月卡车","fullTimeMonthCard"),         //南投项目引入的车卡类型
    LIMIT_MONTH(14,"限时段月卡车","limitTimeMonthCard"),       //南投项目引入的车卡类型
    OVERTIME_CHARGE(15,"超时收费","overTimeCharge"),         //南投项目引入的车卡类型
    RELEASE(16,"手动放行","releaseCar"),                   //南投项目引入的车卡类型

    SENIOR_MANAGEMENT(17,"高级管理卡","seniorManagementCard"),                  //富士车卡类型
    MANAGEMENT(18,"管理卡","managementCard"),                  //富士车卡类型
    CONSUME(19,"消费卡","consume"),                  //富士车卡类型
    EXCEPTION(20,"非法开闸","consume"),                  //富士车卡类型

    TIME_OUT_PAY(97,"超时收费","timeOutPay"),
    CARD_TYPE_NULL(98, "Null_Card_Type", "nullCardType"),
    CARD_TYPE_NOTHING(99, "Nothing_Card_Type", "nothingCardType");

    private static final Map<Integer, VehicleCardTypeEnum> map;
    private static final Map<String, VehicleCardTypeEnum> mapForDescription;
    private static final Map<Integer, VehicleCardTypeEnum> mapForKeTop;
    private static final Map<Integer, VehicleCardTypeEnum> mapForFuShi;

    private static final Map<Integer, VehicleCardTypeEnum> mapForCoSon;
    static {
        map = new HashMap<>();
        for (VehicleCardTypeEnum vehicleCardTypeEnum : VehicleCardTypeEnum.values()) {
            map.put(vehicleCardTypeEnum.getId(), vehicleCardTypeEnum);
        }

        mapForDescription = new HashMap<>();
        for (VehicleCardTypeEnum vehicleCardTypeEnum : VehicleCardTypeEnum.values()) {
            mapForDescription.put(vehicleCardTypeEnum.getDescription(), vehicleCardTypeEnum);
        }

        //富士车卡类型转换
        mapForFuShi = new HashMap<>();
        mapForFuShi.put(5, MONTH);
        mapForFuShi.put(6, MONTH);
        mapForFuShi.put(7, MONTH);
        mapForFuShi.put(8, MONTH);
        mapForFuShi.put(28, TEMPORARILY);
        mapForFuShi.put(9, STORE);
        mapForFuShi.put(10, STORE);
        mapForFuShi.put(11, STORE);
        mapForFuShi.put(12, STORE);


        //科拓车卡类型转换
        mapForKeTop = new HashMap<>();
        mapForKeTop.put(0, TEMPORARILY);
        mapForKeTop.put(1, VIP);
        mapForKeTop.put(2, MONTH);
        mapForKeTop.put(3, STORE);
        mapForKeTop.put(4, HOUR_RENT);
        mapForKeTop.put(5, PROPERTY_RIGHT);
        mapForKeTop.put(6, COUNTING);
        mapForKeTop.put(7, HONORED_GUEST);
        mapForKeTop.put(8, EMPLOYEE);
        mapForKeTop.put(9, TEMPORARY_BOOKING);
        mapForKeTop.put(10, OWNER_TEMPORARY);

        //科松车卡类型转换
        mapForCoSon = new HashMap<>();
        mapForCoSon.put(3651,TEMPORARILY);
        mapForCoSon.put(3650,TEMPORARILY);
        mapForCoSon.put(3649,TEMPORARILY);
        mapForCoSon.put(3648,TEMPORARILY);
        mapForCoSon.put(3652,MONTH);
        mapForCoSon.put(3653,MONTH);
        mapForCoSon.put(3654,MONTH);
        mapForCoSon.put(3655,MONTH);
        mapForCoSon.put(3661,MONTH);
        mapForCoSon.put(3662,MONTH);
        mapForCoSon.put(3663,MONTH);
        mapForCoSon.put(3664,MONTH);
        mapForCoSon.put(3656,FREE);
        mapForCoSon.put(3657,STORE);
        mapForCoSon.put(3660,TIME_OUT_PAY);

    }

    private final Integer id;

    private final String description;

    private final String code;

    public static VehicleCardTypeEnum[] validVehicleCardTypeEnum() {
        return Stream.of(VehicleCardTypeEnum.values())
                .filter(vehicleCardTypeEnum -> CARD_TYPE_NULL != vehicleCardTypeEnum && CARD_TYPE_NOTHING != vehicleCardTypeEnum)
                .toArray(VehicleCardTypeEnum[]::new);
    }

    public static VehicleCardTypeEnum toVehicleCardTypeEnum(Integer id) {
        return Optional.ofNullable(map.get(id)).orElse(CARD_TYPE_NOTHING);
    }

    public static VehicleCardTypeEnum toVehicleCardTypeEnum(String description) {
        return Optional.ofNullable(mapForDescription.get(description)).orElseThrow(() -> new BusinessException("非法的车卡类型名称"));
    }

    public static VehicleCardTypeEnum toVehicleCardTypeEnumByKeTop(Integer id) {
        //参考科拓的文档，把其车卡类型相应转换为车行中台的车卡类型
        if (Objects.isNull(id)) {
            return CARD_TYPE_NULL;
        }
        return Optional.ofNullable(mapForKeTop.get(id)).orElse(CARD_TYPE_NOTHING);
    }

    public static VehicleCardTypeEnum toVehicleCardTypeEnumByFuShi(Integer id) {
        //参考富士的文档，把其车卡类型相应转换为车行中台的车卡类型
        if (Objects.isNull(id)) {
            return CARD_TYPE_NULL;
        }
        return Optional.ofNullable(mapForFuShi.get(id)).orElse(CARD_TYPE_NOTHING);
    }

    public static VehicleCardTypeEnum toVehicleCardTypeEnumByCoSon(Integer id) {
        //参考科松的文档，把其车卡类型相应转换为车行中台的车卡类型
        if (Objects.isNull(id)) {
            return CARD_TYPE_NULL;
        }
        return Optional.ofNullable(mapForCoSon.get(id)).orElse(CARD_TYPE_NOTHING);
    }


    public static Integer[] keTopCardType() {
        return mapForKeTop.keySet().toArray(new Integer[0]);
    }

}
